﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Formatters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using 旅游管理系统.Dtos;
using 旅游管理系统.Models;
using 旅游管理系统.Services;

namespace 旅游管理系统.Controllers
{
    [Route("api/touristRoutes/{touristRouteId}/pictures")] //"api/touristRoutes/{touristRouteId}/pictures"来作为 控制器的 URL的模板路径，通过参数 {touristRouteId}来获得某条旅游路线的所有图片
    [ApiController]
    public class TouristRoutePicturesController : ControllerBase
    {
        private ITouristRouteRepository _touristRouteRepository;
        private IMapper _mapper;

        public TouristRoutePicturesController(ITouristRouteRepository touristRouteRepository, IMapper mapper)
        {
            _touristRouteRepository = touristRouteRepository ??
                throw new ArgumentNullException(nameof(touristRouteRepository));
            _mapper = mapper ??
                throw new ArgumentNullException(nameof(mapper));
        }

        //创建获得图片的 action函数
        [HttpGet]
        public async Task<IActionResult> GetPictureListForTouristRouteAsync(Guid touristRouteId) //此处的 touristRouteId 要与控制器的UIL的模板路径中的 touristRouteId相对应
        {
            if (!await _touristRouteRepository.TouristRouteExistsAsync(touristRouteId))
            {
                return NotFound("旅游路线不存在");  //404
            }

            var picturesFromRepo = await _touristRouteRepository.GetPicturesByTouristRouteIdAsync(touristRouteId);
            if (picturesFromRepo == null || picturesFromRepo.Count() <= 0)
            {
                return NotFound("照片不存在");
            }

            return Ok(_mapper.Map<IEnumerable<TouristRoutePictureDto>>(picturesFromRepo));
        }

        //获得单一数据
        [HttpGet("{pictureId}", Name = "GetPicture")]
        public async Task<IActionResult> GetPictureAsync(Guid touristRouteId, int pictureId) //在 API 中存在嵌套关系时，要先获得父资源后才能获得子资源，如果连父资源都不存在，最好不要暴露子资源
        {
            //先判断父资源是否存在
            if (!await _touristRouteRepository.TouristRouteExistsAsync(touristRouteId))
            {
                return NotFound("旅游路线不存在");  //404
            }
            //凭借图片的 Id获得图片
            //从数据仓库 Repository中获得图片
            var pictureFromRepo = await _touristRouteRepository.GetPictureAsync(pictureId);
            if (pictureFromRepo == null)
            {
                return NotFound("相片不存在");
            }
            return Ok(_mapper.Map<TouristRoutePictureDto>(pictureFromRepo));
        }

        [HttpPost]
        public async Task<IActionResult> CreateTouristRoutePictureAsync(
            [FromRoute] Guid touristRouteId,      //因为这个参数是从 Url中获取的，所以要使用 [FromRoute]
            [FromBody] TouristRoutePictureForCreationDto touristRoutePictureForCreationDto)
        {
            //先判断父资源是否存在
            if (!await _touristRouteRepository.TouristRouteExistsAsync(touristRouteId))
            {
                return NotFound("旅游路线不存在");  //404
            }
            var pictureModel = _mapper.Map<TouristRoutePicture>(touristRoutePictureForCreationDto);
            //调用数据仓库
            _touristRouteRepository.AddTouristRoutePicture(touristRouteId, pictureModel);
            //保存数据库
            await _touristRouteRepository.SaveAsync();
            //输出数据
            var pictureToReturn = _mapper.Map<TouristRoutePictureDto>(pictureModel);
            //使用  CreatedAtRoute 来完成这个数据的返回
            return CreatedAtRoute(
                "GetPicture",
                new
                {
                    touristRouteId = pictureModel.TouristRouteId,
                    pictureId = pictureModel.Id
                },
                pictureToReturn
            );
        }

        [HttpDelete("{pictureId}")]
        public async Task<IActionResult> DeletePictureAsync(
            [FromRoute] Guid touristRouteId,
            [FromRoute] int pictureId
        )
        {
            if (!await _touristRouteRepository.TouristRouteExistsAsync(touristRouteId))
            {
                return NotFound("旅游路线不存在");
            }

            var picture = await _touristRouteRepository.GetPictureAsync(pictureId);
            _touristRouteRepository.DeleteTouristRotuePicture(picture);
            await _touristRouteRepository.SaveAsync();

            return NoContent();
        }
    }
}
